﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TDT.Land.Entity;
using TDT.Land.BusinessLogic;
using TDT.Land.Utilities;

namespace TDT.Land.Web.Controllers
{
    public class MenuController : BaseController
    {
        //
        // GET: /Menu/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetMenuView()
        {
            
            var lstParentMenu = AdminRepo.Instance.GetAllMenu(menuViewSite.User).OrderBy(d=>d.MenuOrder).ToList();
            ViewBag.lstMenu = lstParentMenu;
            return PartialView("UC_Menu_html");
        }

        [CustomAuthorize(Roles = "3")]
        public ActionResult InsertMenu(Menu itemMenu)
        {
            var str_return = "01";
            bool statusSave = false;
            string controller = Request.QueryString["controllerField"];
            if (itemMenu.MenuID > 0)
                statusSave = AdminRepo.Instance.UpdateMenu(itemMenu, UserName);
            else
                statusSave = AdminRepo.Instance.addNewMenu(itemMenu, UserName);
            if (statusSave)
                str_return = "00";

            return Content(str_return);
        }

        [CustomAuthorize(Roles = "3")]
        public ActionResult ManagerMenu()
        {
            var lstParentMenu = AdminRepo.Instance.GetAllMenu(menuViewSite.User).OrderBy(d => d.MenuOrder).ToList();
            ViewBag.lstMenu = lstParentMenu;
            return View();
        }

        [CustomAuthorize(Roles = "3")]
        public ActionResult openEditMenu(string id)
        {
            var currentMenuParent = 0;
            var currentLink = "";

            #region get menu for update

            int menuId = !string.IsNullOrEmpty(id) ? int.Parse(id) : 0;
            var modelMenu = AdminRepo.Instance.GetMenuById(menuId);
            if (modelMenu != null)
            {
                currentLink = modelMenu.MenuUrl;
                currentMenuParent = modelMenu.ParentID != null ? (int)modelMenu.ParentID : 0;
            }
            ViewBag.MenuInfo = modelMenu;

            #endregion

            #region get parent menu
            var lstParentMenu = AdminRepo.Instance.GetAllParentMenu(menuViewSite.User).ToList();
            List<SelectListItem> lstSelect = new List<SelectListItem>();
            lstSelect.Add(new SelectListItem { Text = "none", Value = "0" });
            foreach (var menuParent in lstParentMenu)
            {
                SelectListItem sel_ItemMenu = new SelectListItem();                
                sel_ItemMenu.Text = menuParent.MenuName;
                sel_ItemMenu.Value = menuParent.MenuID.ToString();
                if (currentMenuParent == menuParent.MenuID)
                    sel_ItemMenu.Selected = true;
                lstSelect.Add(sel_ItemMenu);
            }

            ViewBag.lstMenuParent = lstSelect;
            #endregion

            #region Get bai viết for add link
            string buidingType = CategoryType.W_BUIDING_TYPE.ToString();
            var pro_buiding = AdminRepo.Instance.GetProBuildingByType(buidingType).OrderByDescending(m => m.DateCreate).ToList();

            List<SelectListItem> lstSelectBaiViet = new List<SelectListItem>();
            lstSelectBaiViet.Add(new SelectListItem { Text = "---", Value = "/" });
            foreach (var BaiViet in pro_buiding)
            {
                SelectListItem sel_ItemBV = new SelectListItem();                
                sel_ItemBV.Text = BaiViet.BuildingName.Length > 20 ? BaiViet.BuildingName.Substring(0, 17) + "..." : BaiViet.BuildingName;
                sel_ItemBV.Value = BaiViet.Code;
                if (currentLink.Contains(BaiViet.Code))
                    sel_ItemBV.Selected = true;
                lstSelectBaiViet.Add(sel_ItemBV);
            }

            ViewBag.lstBaiViet = lstSelectBaiViet;

            #endregion

           

            return View();
        }


        public ActionResult getMenuLeft()
        {
            var lstParentMenu = AdminRepo.Instance.GetMenuChil(3).ToList();
            ViewBag.lstMenu = lstParentMenu;

            return PartialView("US_UC_MenuLeft");
        }

    }
}
